package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.BrandMapper;
import com.cskaoyan.catmall.model.bo.BrandCreateBo;
import com.cskaoyan.catmall.model.bo.BrandDeleteBo;
import com.cskaoyan.catmall.model.bo.BrandListBo;
import com.cskaoyan.catmall.model.bo.BrandUpdateBo;
import com.cskaoyan.catmall.model.vo.BrandCreateVo;
import com.cskaoyan.catmall.model.vo.BrandListInnerListVo;
import com.cskaoyan.catmall.model.vo.BrandListVo;
import com.cskaoyan.catmall.service.BrandService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 创建日期: 2022-01-07-13-33
 * author : WQW
 * description: 品牌相关的操作
 */

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;


    /**
     * @param
     * @param brandListBo
     * @return com.cskaoyan.catmall.model.vo.BrandListVo
     * @description 查询品牌商信息并分页
     * @author WQW
     * @date 2022/1/7 20:06
     */
    @Override
    public BrandListVo getBrandList(BrandListBo brandListBo) {
        Integer page = brandListBo.getPage();// 页码
        Integer limit = brandListBo.getLimit();// 数据量
        // 通过PageHelper插件，辅助完成分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        List<BrandListInnerListVo> brandListInnerListVos = brandMapper.selectBrandList(brandListBo);
        // 通过PageHelper获得分页相关信息,使用其有参构造方法，形参来源于数据库的list
        PageInfo<BrandListInnerListVo> pageInfo = new PageInfo<>(brandListInnerListVos);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return new BrandListVo(total, pages, limit, page, brandListInnerListVos);
    }


    /**
     * @param
     * @param brandCreateBo
     * @return com.cskaoyan.catmall.model.vo.BrandCreateVo
     * @description 新增品牌商信息到数据库
     * @author WQW
     * @date 2022/1/7 20:16
     */
    @Override
    @Transactional
    public BrandCreateVo createBrand(BrandCreateBo brandCreateBo) {
        BrandCreateVo brandCreateVo = new BrandCreateVo();
        List<String> nameList = brandMapper.selectBrandName(null);
        if (nameList.contains(brandCreateBo.getName())) {
            return brandCreateVo;
        }
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
        String format = dateFormat.format(date);
        brandCreateVo.setDesc(brandCreateBo.getDesc());
        brandCreateVo.setFloorPrice(brandCreateBo.getFloorPrice());
        brandCreateVo.setName(brandCreateBo.getName());
        brandCreateVo.setPicUrl(brandCreateBo.getPicUrl());
        brandCreateVo.setAddTime(format);
        brandCreateVo.setUpdateTime(format);
        brandMapper.insertBrand(brandCreateVo);
        return brandCreateVo;
    }

    /**
     * @param
     * @param brandDeleteBo
     * @return int
     * @description 删除指定品牌商信息
     * @author WQW
     * @date 2022/1/7 20:44
     */
    @Override
    @Transactional
    public int deleteBrand(BrandDeleteBo brandDeleteBo) {
        Integer id = brandDeleteBo.getId();
        String picUrl = brandDeleteBo.getPicUrl();
        String key = picUrl.substring(picUrl.lastIndexOf("/") + 1);
        try {
            brandMapper.deleteBrandById(id);
            brandMapper.deleteStorageByKey(key);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 404;
        }
        return 200;
    }

    /**
     * @param
     * @param updateBo
     * @return int
     * @description 修改品牌商信息
     * @author WQW
     * @date 2022/1/9 21:42
     */
    @Override
    @Transactional
    public int updateBrand(BrandUpdateBo updateBo) {
        List<String> nameList = brandMapper.selectBrandName(updateBo.getId());
        if (nameList.contains(updateBo.getName())) {
            return 404;
        }
        try {
            brandMapper.updateBrand(updateBo);
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 404;
        }
        return 200;
    }
}

